package com.itboye.sunsun;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.ConfigBean;
import com.itboye.sunsun.beans.SttingTelBeans;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.dialog.WarnDialog;
import com.itboye.sunsun.luntan.LunTanDetailActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.product.center.ProductCenterHomeActivity;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.UIAlertView02;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsunhome.R;
import com.itboye.sunsunhome.util.Byalert;
import com.itboye.sunsunhome.www.aq.DeviceActivity;

import java.util.ArrayList;

import static com.itboye.sunsun.volley.MyJsonRequest.Builder.getVersionCode;

/**
 * 首页
 * 
 * @author liu
 * 
 */
public class HomeFragmentH extends BaseFragment implements OnClickListener {

	private LinearLayout layoutLunTan, layoutDevice, layoutProcunter;
	private Intent intent;

	private TextView tvtel, title;
	private String resultData;

	private String PRODUCER_PHONE;
	private ArrayList<ConfigBean> arConfig;
	private UIAlertView02 delDialog;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return inflater.inflate(R.layout.acyivity_collection, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		// getShuiZuChildForum();
		// getShuidForum();

		// 长按可以识别微信二维码
//		title.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
////				 Intent intent=new
////				 Intent(getActivity(),HomeEnglishActivity.class);
////				 startActivity(intent);
//			}
//		});
		getVersionAvaliable();

		pullDown();
		IntentFilter intentFilterAvart = new IntentFilter("phone");
		getActivity().registerReceiver(avartBroadcastReceiver,
				intentFilterAvart);
		tvtel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
						+ PRODUCER_PHONE));
				startActivity(intent);
			}
		});

	}

	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			resultData = (String) SPUtils.get(App.ctx, null, SPContants.PHONE,
					"");
			tvtel.setText(getString(R.string.product_detail));
//			tvtel.setText("TEL:" + resultData);
		};
	};

	private void pullDown() {

		MyJsonRequest.Builder<SttingTelBeans> builder = new MyJsonRequest.Builder<SttingTelBeans>()
				.apiVer("100").typeKey("BY_Supplement_config")
				.requestListener(new XRequestListener<SttingTelBeans>() {

					@Override
					public void onResponse(final SttingTelBeans bean) {
						System.out.println("<><><><><><>><>>"
								+ bean.getCustomer_phone().getValue());
						// onPullDownFinish(adapter, xlistview, bean);
						// tvtel.setText("TEL:"+bean.getValue());

						SPUtils.put(App.ctx, null, SPContants.PHONE, bean
								.getCustomer_phone().getValue());
						resultData = (String) SPUtils.get(App.ctx, null,
								SPContants.PHONE, "");

						SPUtils.put(App.ctx, null, SPContants.PRODUCT_PHONE,
								bean.getProduct_phone().getValue());
						PRODUCER_PHONE = (String) SPUtils.get(App.ctx, null,
								SPContants.PRODUCT_PHONE, "");
						// 发送广播通知更改客服电话
						Intent intent2 = new Intent("phone");
						App.ctx.sendBroadcast(intent2);

					}

				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						// onPullDownError(adapter, xlistview, exception, code,
						// msg);
					}
				});

		MyJsonRequest<SttingTelBeans> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	public void getVersionAvaliable() {

		MyJsonRequest.Builder<ArrayList<ConfigBean>> builder = new MyJsonRequest.Builder<ArrayList<ConfigBean>>()
				.apiVer("100").typeKey("BY_Config_app")
				.requestListener(new XRequestListener<ArrayList<ConfigBean>>() {

					@Override
					public void onResponse(ArrayList<ConfigBean> bean) {
						arConfig = bean;
						int newVersion = 0;
						ConfigBean newConfigBean = null;
						for (ConfigBean configBean : bean) {
							if (configBean.getName().equals("android_fuwu_version")) {
								newConfigBean = configBean;
								newVersion = Integer.parseInt(configBean.getValue().replace(".", ""));
								break;
							}
						}
						int oldVersion=getVersionCode();
						if (newVersion > oldVersion) {
							onManager(newConfigBean);
						}
					}

				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
												String msg) {
						System.out.println(msg + "----" + code);
					}
				});

		MyJsonRequest<ArrayList<ConfigBean>> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}

	public void onManager(ConfigBean appUpdateBean) {
		delDialog = new UIAlertView02(getActivity(), appUpdateBean.getValue(), "有更新啦!", "以后再说", "立即更新");
		delDialog.show();

		delDialog.setClicklistener(new UIAlertView02.ClickListenerInterface() {

			public void doLeft() {
				delDialog.dismiss();
			}

			public void doRight() {
				String url = "";
				delDialog.dismiss();
				for (ConfigBean configBean : arConfig) {
					if ("android_fuwu_url".equals(configBean.getName())) {
						url = configBean.getValue();
						break;
					}
				}
				if (!url.equals("")) {
					Uri uri = Uri.parse(url);
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setData(uri);
					startActivity(intent);
				}else{
					Byalert.alert("下载地址有误");
				}
			}
		});
//        UpdateManager manager = new UpdateManager(this,
//                 "ceshi ",
//                "http://a.app.qq.com/o/simple.jsp?pkgname=com.itboye.sunsunhome","1.1.3");
//        manager.checkUpdateInfo();
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.layoutLunTan:
			intent = new Intent(getActivity(), LunTanDetailActivity.class);
			intent.putExtra("fid", "1");
			intent.putExtra("title", "水族论坛");
			intent.putExtra("subforum", "4");
			startActivity(intent);
			break;
		case R.id.layoutDevice:

			final String uid = (String) SPUtils.get(getContext(), null,
					SPContants.USER_ID, "");

			boolean flag = (boolean) SPUtils.get(getContext(),
					"yubaomu_first_use", uid, true);
			if (flag == false) {
				App.ctx.init();
				startActivity(new Intent(getActivity(), DeviceActivity.class));
			} else {

				new WarnDialog("条款与申明", getString(R.string.yubaomu_warning),
						new WarnDialog.OnDialogSelectListener() {

							@Override
							public void onDialoSelect(boolean isAgree,
									boolean noTip) {
								if (isAgree) {
									App.ctx.init();
									startActivity(new Intent(getActivity(),
											
											DeviceActivity.class));

								} else {

								}
								if (noTip) {
									SPUtils.put(getContext(),
											"yubaomu_first_use", uid, false);
								}
							}
						}).show(getChildFragmentManager(), null);

			}
			break;
		case R.id.layoutProcunter:
//			LoginController.goToProduct(getActivity(), null);
			 Intent intent=new
			 Intent(getActivity(),ProductCenterHomeActivity.class);
			 startActivity(intent);
			break;
		default:
			break;
		}
	}

}
